﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LockCrypt.Core {
    /// <summary>
    /// A field whose name or value can be matched by a string which it starts with, ends with or contains.
    /// </summary>
    public class WildcardField {
        /// <summary>
        /// Gets or sets the name of the field.
        /// </summary>
        /// <value>The name of the field.</value>
        public WildcardValue FieldName { get; set; }

        /// <summary>
        /// Gets or sets the field value.
        /// </summary>
        /// <value>The field value.</value>
        public WildcardValue FieldValue { get; set; }

        /// <summary>
        /// Initializes a new <see cref="WildcardField"/>.
        /// </summary>
        public WildcardField() {

        }

        /// <summary>
        /// Returns a localized <see cref="System.String"/> describing this wildcard.
        /// </summary>
        /// <returns>
        /// A localized String describing this wildcard.
        /// </returns>
        public override string ToString() {
            string prettyWildcard;
            if(FieldName != null) {
                string nameMatch = TranslateWildcardType(FieldName.WildcardType);
                if(FieldValue != null) {
                    string valueMatch = TranslateWildcardType(FieldValue.WildcardType);
                    prettyWildcard = I18nUtils.GetString("Strings", "NameAndValueContainsFmt", nameMatch, FieldName.Value, valueMatch, FieldValue.Value);
                } else { // name, but no value
                    prettyWildcard = I18nUtils.GetString("Strings", "NameMatchFmt", nameMatch, FieldName.Value);
                }
            } else if(FieldValue != null) { // value, no name
                string valueMatch = TranslateWildcardType(FieldValue.WildcardType);
                prettyWildcard = I18nUtils.GetString("Strings", "ValueMatchFmt", valueMatch, FieldValue.Value);
            } else { // neither name nor value
                prettyWildcard = base.ToString();
            }
            return prettyWildcard;
        }

        /// <summary>
        /// Translates the type of the wildcard.
        /// </summary>
        /// <param name="wildcardType">Type of the wildcard.</param>
        /// <returns>A localized verison of the type</returns>
        public static string TranslateWildcardType(WildcardValue.WildcardTypes wildcardType) {
            string lozalizedType;
            switch(wildcardType) {
                case WildcardValue.WildcardTypes.Contains:
                    lozalizedType = I18nUtils.GetString("Strings", "Contains");
                    break;
                case WildcardValue.WildcardTypes.StartsWith:
                    lozalizedType = I18nUtils.GetString("Strings", "StartsWith");
                    break;
                case WildcardValue.WildcardTypes.EndsWith:
                    lozalizedType = I18nUtils.GetString("Strings", "EndsWith");
                    break;
                default:
                    lozalizedType = string.Empty;
                    break;
            }
            return lozalizedType;
        }

        /// <summary>
        /// Clones this WildcardField.
        /// </summary>
        /// <returns>A clone of this WildcardField</returns>
        public WildcardField Clone() {
            WildcardField clone = new WildcardField() {
                FieldName = this.FieldName == null ? null : this.FieldName.Clone(),
                FieldValue = this.FieldValue == null ? null : this.FieldValue.Clone()
            };
            return clone;
        }
    }
}
